


//============================================登录界面
// import '//at.alicdn.com/t/font_2227089_qlt50y8821i.css'
import './Login.css'
import IconFont from "../logo";
import React, {Component} from 'react';
import {observer,inject} from "mobx-react";
import axios from '../../utils/http'
import {getsessionstorage,setsessionstorage,removeSessiontorage} from "../../utils/utils"
import {Button, Divider, Input, Space,message} from 'antd';
import { EyeInvisibleOutlined, EyeTwoTone,UnlockOutlined ,UserOutlined,SolutionOutlined } from '@ant-design/icons';
import {color} from "echarts/src/export";
// import './login'
function pro(data) {
  return   new Promise((resolve, reject) => {

      if(data.data.code==200){
          message.success('登录成功')
          console.log(data.data.data)
          setsessionstorage('id',data.data.data.adminId)
          setsessionstorage('yong',data.data.data.adminUsername)



              for(let i=0;i<data.data.data.premiereMeuns.length;i++){
                  if(data.data.data.premiereMeuns[i].premiereParentid==null){
                      // 遍历每一项
                      // 如果父级id为空  就去搜寻其余的数据的父级id是否等于它的premiereMeunid
                      // 如果有等于它的  就新建一个children对象
                      // 并把数据接入其中
                      let children=[]
                      for(let j=0;j<data.data.data.premiereMeuns.length;j++){
                          if(data.data.data.premiereMeuns[i].premiereMeunid==data.data.data.premiereMeuns[j].premiereParentid){
                              children.push(data.data.data.premiereMeuns[j])
                          }
                      }
                      data.data.data.premiereMeuns[i].children=children
                  }
              }

                 let arry= data.data.data.premiereMeuns
              for(let k=0;k<arry.length;k++){
                  if(arry[k].children && arry[k].children!=''){
                      for(let f=0;f<arry[k].children.length;f++){
                          for(let y=1;y<arry.length;y++){
                              if(arry[k].children[f].premiereMeunid===arry[y].premiereMeunid){
                                  arry.splice(y,1)
                              }
                          }
                      }
                  }
              }

          resolve(arry)
      }
      else{
          reject(500)
          message.error('登录失败')
      }
    })

}
String.prototype.compare = function (str) {
    //不区分大小写
    if (this.toLowerCase() == str.toLowerCase()) {
        return true;
    } else {
        return false;
    }
};
@inject('lu')
@observer
  class Login extends Component {
    constructor(props) {
        super(props);
        this.state = {
            user:'',
            pwd:'',
            yan:'',
            tutu:'',
            rightYan:'',
        };
    }
    componentDidMount() {
        axios.get('http://172.16.10.31:8771/mm-certification/auth/checkCode').then(
            data=>{
                this.setState({
                    tutu:data.data.data,
                    rightYan:data.data.data
                })
            }
        )
    }
    // 获取验证码
    getTu=()=>{
     axios.get('http://172.16.10.31:8771/mm-certification/auth/checkCode').then(
         data=>{
             this.setState({
                 tutu:data.data.data
             })
         }
     )
     //    const that=this
     //    new Promise((resolve, reject) => {
     //        that.setState({
     //            tutu:''
     //        })
     //        resolve(200)
     //    }).then(data=>{
     //        console.log(data)
     //        setTimeout(function () {
     //            that.setState({
     //                tutu:'http://172.16.10.31:8771/mm-certification/auth/checkCode'
     //            })
     //        },1000)
     //
     //    })


    }
    userLogin=(e)=>{
        this.setState({
            user:e.target.value
        })
    }

    pwdLogin=(e)=>{
        this.setState({
            pwd:e.target.value
        })
    }
    yanLogin=(e)=>{
        this.setState({
            yan:e.target.value
        })
    }
    //登录界面
    loginU=()=>{
       if(this.state.yan.compare(this.state.tutu)){
           axios.post('/mm-certification/auth/getToken',{
               adminUsername:this.state.user,
               adminPassword:this.state.pwd,
               code:this.state.yan
           }).then(async data=>{

               // let arry=data.data.data.premiereMeuns

               this.props.lu.getLoginer(data.data)
               let pro1=await pro(data)
               this.props.lu.loginUser(pro1).then(data=>{
                   this.props.history.push('/Home')

               })

           })

       }else{
           alert('验证码错误')
       }




    }
    render() {
        return (
            <div className='Login'>
                {/*<canvas id="canvas"></canvas>*/}

                <img src="" alt=""/>
                {/*<h1><IconFont type="icon-pinduoduo"></IconFont>拼夕夕在线坑钱系统</h1>*/}
                <div className='kuan'>
                    <IconFont type="icon-pinduoduo" style={{fontSize: '5rem', marginLeft: '1rem',float:'left'}} />
                    <h1><span>拼夕夕在线坑钱系统</span></h1>
                    <div  className='lan'>
                        <Input size="large" value={this.state.user}
                               onChange={this.userLogin}
                               placeholder="请输入用户名" prefix={<UserOutlined />} /></div>

                    <br/>
                   <div className='lan'>
                       <Space direction="vertical">
                           <Input.Password
                           onChange={this.pwdLogin}
                           value={this.state.pwd}
                           placeholder="请输入密码"
                           prefix={<UnlockOutlined />}
                           iconRender={visible => (visible ? <EyeTwoTone /> : <EyeInvisibleOutlined />)}
                       />
                       </Space>
                   </div>

                    <div className='lan'>
                        <Input size="large" value={this.state.yan}
                               onChange={this.yanLogin}
                               className='lang'
                               placeholder="请输入验证码" prefix={<SolutionOutlined />} />

                        <span className='tutu' onClick={this.getTu}>{this.state.tutu}</span>

                    </div>


                    <Button type="primary" onClick={this.loginU}>登录</Button>


                </div>

            </div>
        )
    }
}
export default Login